package x.amadeus.interceptor;

import java.io.PrintWriter;
import java.security.ProviderException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jasig.cas.client.validation.Assertion;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;



import x.amadeus.admin.domain.Admin;
import x.amadeus.staff.domain.SysUser;


public class AdminLoginInterceptor implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
/////loooook!!!!!
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object arg2) throws Exception {
		Admin admin = (Admin) request.getSession().getAttribute("session_admin");
		if(admin != null)
		{
			return true;
		}else{
            PrintWriter out = response.getWriter();  
            out.print("loseSession");//session失效
            out.flush();
			return false;
		}
/*		Object object = request.getSession().getAttribute("_const_cas_assertion_");
		System.out.println("1");
        if (object != null) {
        	System.out.println("2");
            Assertion assertion = (Assertion) object;
            String loginName = assertion.getPrincipal().getName();
            SysUser user=(SysUser)request.getSession().getAttribute("user");
	        if (user == null) {
	        	System.out.println("3");
	        		response.sendRedirect("https://sso.ruijie.net:8443/fipm/account/ErrorPage.jsp?sys=trainee");
	        		return false;
	        	}
	        request.getSession().setAttribute("user", user);
	        return true;
	    }
        System.out.println("4");
        PrintWriter out = response.getWriter();  
        out.print("loseSession");//session失效
        out.flush();
		//response.sendRedirect("https://sso.ruijie.net:8443/fipm/account/ErrorPage.jsp?sys=trainee");

        return false;*/
	}
}
